import { reqShopcarList, reqDeleteShopCart, reqChangeShopCart } from "@/api";
const actions = {
    async getShopcarList({ commit }) {
        let result = await reqShopcarList()
        if (result.code == 200) {
            commit("GETSHOPCARLIST", result.data)
        }
    },
    //删除某个商品
    async deleteShopCart({ commit }, skuId) {
        const result = await reqDeleteShopCart(skuId)
        if (result.code == 200) {
            return "ok";
        } else {
            return Promise.reject(new Error("fail"))
        }
    },
    //切换某个商品的选中状态
    async changeShopCartChecked({ commit }, { skuId, isChecked }) {
        const result = await reqChangeShopCart(skuId, isChecked)
        if (result.code == 200) {
            return "ok"
        } else {
            return Promise.reject(new Error("fail"))
        }
    },
    // //删除所有的被选中的商品
    deleteAllShopCartChecked({ dispatch, getters }) {
        let allPromise = [];
        getters.shopList.cartInfoList.forEach((item) => {
            let result = item.isChecked == 1 ? dispatch("deleteShopCart", item.skuId) : ""
            allPromise.push(result)
        })
        return Promise.all(allPromise)
    },
    //全选按钮的状态切换
    updateAllChecked({ dispatch, getters }, event) {
        let allPromise = [];
        let isChecked = event.target.checked ? "1" : "0"
        getters.shopList.cartInfoList.forEach((item) => {
            let result = dispatch("changeShopCartChecked", { skuId: item.skuId, isChecked })
            allPromise.push(result)
        })
        return Promise.all(allPromise)
    }
};
const mutations = {
    GETSHOPCARLIST(state, shopList) {
        state.shopList = shopList
    }
};
const state = {
    shopList: []
};
const getters = {
    shopList(state) {
        return state.shopList[0] || {}
    }
};
export default {
    actions,
    mutations,
    state,
    getters
}